import Vue from 'vue'
import router from './router'
import env from './env'
import App from './App.vue'
import axios from 'axios';
// 将axios挂载到vue实例上
import VueAxios from 'vue-axios';
import store from './store/index';
import {
  Message,
  MessageBox
} from 'element-ui';
import VueLazyLoad from 'vue-lazyload';
import VueCookie from 'vue-cookie';
import 'element-ui/lib/theme-chalk/index.css'

/**
 * import和require的区别
 * import是预编译加载，在编译的时候就被加载进来了
 * require是程序从上到下执行的时候才会被加载
 */
const mock = false;
if (mock) {
  require('./mock/api');
}

//根据前端的跨域方式作调整，接口代理的方式才能不写域名，即前端地址后后台接口是同一域名
axios.defaults.baseURL = '/api';
axios.defaults.timeout = 8000;
// 根据环境变量获取不同的请求地址
// axios.defaults.baseURL = env.baseURL;
//接口错误拦截
axios.interceptors.response.use(function (response) {
  let res = response.data;
  let path = location.hash;

  if (res.status === 0) {
    return res.data;
  } else if (res.status === 10) {
    //未登录
    if (path != '#/index') {
      window.location.href = '/#/login';
      return Promise.reject(res);
    }
  } else {
    Message.warning(res.msg);
    return Promise.reject(res);
  }
}, (error) => {
  let res = error.response;
  Message.error(res.data.message);
  // 抛出异常
  return Promise.reject(error);
});


Vue.use(VueCookie);

Vue.use(VueAxios, axios)
Vue.use(VueLazyLoad, {
  loading: '/imgs/loading-svg/loading-bars.svg'
});

Vue.config.productionTip = false
Vue.prototype.$message = Message;
Vue.prototype.$confirm = MessageBox.confirm;

new Vue({
  store,
  router,
  render: h => h(App),
}).$mount('#app')
